import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

import java.util.Set;

public class RedisDemoGeneric {
    public static void main(String[] args) {
        // 1. 连接到 Redis 服务器上.
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        // 2. 从 Redis 连接池中取一个连接出来，并及时释放
        try(Jedis jedis = jedisPool.getResource()){
            // 3. Redis 的各种命令，对应到 jedis 对象的各种方法
            // String pong = jedis.ping();
            // System.out.println(pong);
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
            test5(jedis);


        }
    }

    private static void test5(Jedis jedis) {
        System.out.println("type");
        jedis.flushAll();

        jedis.set("key","111");
        String type = jedis.type("key");
        System.out.println("type: "+type);

        jedis.rpush("key2","111","222","333");
        String type2 = jedis.type("key2");
        System.out.println("type2: "+type2);

        jedis.hset("key3","f1","111");
        String type3 = jedis.type("key3");
        System.out.println("type3: "+type3);

        jedis.sadd("key4","111","222","333");
        String type4 = jedis.type("key4");
        System.out.println("type4: "+type4);

        jedis.zadd("key5",97.6,"zhangsan");
        String type5 = jedis.type("key5");
        System.out.println("type5: "+type5);






    }

    private static void test4(Jedis jedis) {
        System.out.println("expire n ttl");
        jedis.flushAll();
        jedis.set("key","111");
        jedis.expire("key",10);

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        long ttl = jedis.ttl("key");
        System.out.println("time: "+ttl);
    }

    private static void test3(Jedis jedis) {
        System.out.println("keys");
        jedis.flushAll();
        jedis.set("key","111");
        jedis.set("key2","222");
        jedis.set("key3","333");
        jedis.set("key4","444");

        Set<String> keys = jedis.keys("*");
        System.out.println(keys);


    }

    private static void test2(Jedis jedis) {
        System.out.println("exists n del");
        jedis.flushAll();
        jedis.set("key","111");
        jedis.set("key2","222");
        jedis.set("key3","333");

        boolean ret = jedis.exists("key");
        System.out.println(ret);

        long ret2 = jedis.del("key");
        System.out.println(ret2);

        ret = jedis.exists("key");
        System.out.println(ret);

        ret2 = jedis.del("key", "key2", "key3");
        System.out.println("删除了 "+ret2+" 个key");

    }

    private static void test1(Jedis jedis){
        System.out.println("get n set");
        //清空数据库，避免上一组数据影响下一组测试的结果
        jedis.flushAll();
        jedis.set("k1","111");
        jedis.set("k2","222");
        // 设置参数
        SetParams params = new SetParams();
        params.ex(10);
        params.xx();
        jedis.set("k1","333",params);

        String value = jedis.get("k1");
        System.out.println("value = " + value);
        try {
            Thread.sleep(10001);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        value = jedis.get("k1");
        System.out.println("value = " + value);
    }






}
